package guoanmaker.operator.business.service;

import guoanmaker.operator.business.model.OperatorRoundPhotoApp;
import guoanmaker.operator.business.repository.OperatorRoundPhotoAppRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

/**
 * @author huZhaoyu 
 * @ClassName: OperatorRoundPhotoAppService 
 * @Description: OperatorRoundPhotoAppService
 * @date 2017/6/28 上午9:41 
 */
@Service
public class OperatorRoundPhotoAppService implements OperatorRoundPhotoAppServiceInterface {
    private final OperatorRoundPhotoAppRepository OperatorRoundPhotoAppRepository;
    @PersistenceContext
	private EntityManager manager;
    @Autowired
    public OperatorRoundPhotoAppService(OperatorRoundPhotoAppRepository OperatorRoundPhotoAppRepository) {
        this.OperatorRoundPhotoAppRepository = OperatorRoundPhotoAppRepository;
    }

    @Override
    public OperatorRoundPhotoApp saveRoundPhoto(OperatorRoundPhotoApp pp) {
        return OperatorRoundPhotoAppRepository.save(pp);
    }

    @Override
    public List<OperatorRoundPhotoApp> getAllRoundphoto(String cityId) {
        return OperatorRoundPhotoAppRepository.findBycityCode(cityId);
    }

    @Override
    public Page<OperatorRoundPhotoApp> getAllRoundPhotoByPage(Integer page, Integer size) {
        Pageable pageable = new PageRequest(page, size);
        return OperatorRoundPhotoAppRepository.findAll(pageable);
    }

    @Override
    public void deleteRoundPhotoById(String id) {
        OperatorRoundPhotoAppRepository.delete(id);
    }

    @Override
    public OperatorRoundPhotoApp getRoundPhotoById(String id) {
        return OperatorRoundPhotoAppRepository.findOne(id);
    }

    @Override
    public Page<OperatorRoundPhotoApp> getAllRoundPhotoByWeightAndPage(Integer page, Integer size) {
        Sort sort = new Sort(Sort.Direction.DESC, "weight");
        Pageable pageable = new PageRequest(page, size, sort);
        return OperatorRoundPhotoAppRepository.findAll(pageable);
    }
    
    @Override
    public void deleteRoundPhotoAll() {
        OperatorRoundPhotoAppRepository.deleteAll();
    }
    
    @Transactional
	  public void delectAllBycitycode(String cityId){
		  String jpql="delete  from OperatorRoundPhotoApp  u where u.cityId=:cityId";
		  Query query2=manager.createQuery(jpql);//查询商品提供的服务区域
			query2.setParameter("cityId", cityId);
			query2.executeUpdate();
		  
	  }

}
